package com.btl.cygf;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.btl.cygf.MainActivity;
import com.btl.cygf.R;
import com.btl.cygf.TieZiActivity;
import com.btl.sqlite.DbOpService;
import com.btl.sqlite.History;
import com.btl.utils.TimeUtils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class RecordActivity extends Activity implements OnClickListener{
   private ListView mListView;
   private List<History> listItems = new ArrayList<History>(); // 数据源集合
   private String username ;
   private MyListAdapter mAdapter;
   
   ImageView login_backtofragment;
   Button clear;
 //右下角返回
 		@Override
 		public boolean onKeyDown(int keyCode, KeyEvent event) {
 			if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
 				this.finish();
 			}
 			return false;
 		}
		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			requestWindowFeature(Window.FEATURE_NO_TITLE);
			setContentView(R.layout.myrecord);
			login_backtofragment = (ImageView) findViewById(R.id.login_backtofragment);
			login_backtofragment.setOnClickListener(this);
			clear = (Button) findViewById(R.id.clear);
			//代码填写部分
			Intent intent = getIntent();
			username = intent.getStringExtra("id");
			//连接数据库
			DbOpService db= new DbOpService(getApplicationContext());
			//查询数据库  获取list
			List<History> list = db.Query(username);
			
			Map<String , History> map = new HashMap<>();
			
			for(History hs : list) {
				Set<String> set = map.keySet();
				if(!set.contains(hs.getTitle())){
					System.out.println(hs.getTitle());
					map.put(hs.getTitle(), hs);
					listItems.add(hs);
				}
				
			}
			
			
			
			mListView = (ListView) findViewById(R.id.list_record);
			mAdapter = new MyListAdapter();
			mListView.setAdapter(mAdapter);
			
		}
		
		public void clear(View v){
			Builder builder = new Builder(this);
			builder.setTitle("提示");
			builder.setMessage("你确定要删除所有历史记录吗？");
			DialogInterface.OnClickListener dialog = new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					// TODO Auto-generated method stub
					if (arg1 == DialogInterface.BUTTON_POSITIVE) {
						arg0.cancel();
					} else if (arg1 == DialogInterface.BUTTON_NEGATIVE) {
						//连接数据库
						DbOpService db= new DbOpService(getApplicationContext());
						//查询数据库  获取list
						db.Delete(username);
						listItems.clear();
						mAdapter.notifyDataSetChanged();
						Toast.makeText(getApplicationContext(), "删除完成！", 1).show();
					}
				}
			};
			builder.setPositiveButton("取消", dialog);
			builder.setNegativeButton("确定", dialog);
			AlertDialog alertDialog = builder.create();
			alertDialog.show();
		}
	     //点击返回
	 		@Override
	 		public void onClick(View v) {
	 			switch (v.getId()) {
	 			case R.id.login_backtofragment:
	 				this.finish();
	 				break;
	 			}
	 		}
		
		
		/**
		 * listview的适配器MyListAdapter
		 */
		@SuppressWarnings("unused")
		private class MyListAdapter extends BaseAdapter {

			class ListItemView {
				public TextView title;
				public TextView intro;
				public TextView time;
			}


			@Override
			public int getCount() {
				return listItems.size();
			}

			@Override
			public Object getItem(int position) {
				// TODO Auto-generated method stub
				return listItems.get(position);
			}

			@Override
			public long getItemId(int position) {
				// TODO Auto-generated method stub
				return 0;
			}
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				
				mListView.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						//listItems.get(position-1).getVid();-------->当前选中条目的id
						Intent intent = new Intent();
						intent.setClass(RecordActivity.this, TieZiActivity.class);
						intent.putExtra("v_id",listItems.get(position).getVid()+"");
						startActivity(intent);
					}
				});
				// final int selectID = position;
				// 自定义视图
				ListItemView listItemView = null;
				if (convertView == null) {
					listItemView = new ListItemView();
					// 获取list_item布局文件的视图
					convertView = LayoutInflater.from(getApplicationContext()).inflate(
							R.layout.record_item, null);
					// 获取控件对象
					listItemView.title = (TextView) convertView
							.findViewById(R.id.title);
					listItemView.intro = (TextView) convertView
							.findViewById(R.id.jian_jie);
					listItemView.time = (TextView) convertView
							.findViewById(R.id.shi_jian);
					// 设置控件集到convertView
					convertView.setTag(listItemView);
				} else {
					listItemView = (ListItemView) convertView.getTag();
				}
				// listItemView.image.setImageBitmap(new BitmapFactory().decodeByteArray(data, offset, length))
				listItemView.title.setText(listItems.get(position).getTitle());
				listItemView.intro.setText(listItems.get(position).getIntro());
				
				String timeString = TimeUtils.getDate(listItems.get(position).getVtime());
				System.out.println(timeString);
				listItemView.time.setText(timeString);
//				listItemView.time.setText(listItems.get(position).getVtime());
				return convertView;
			}
			
		}
 		
   
   
}
